/***************************************************************************//**
 * @file
 * @brief button callbacks handler
 ******************************************************************************/
#include "main_ui.h"
#include "Ext_Cluster.h"
#include "MLR.h"
extern EmberEventControl emberAfPluginConnectionManagerRejoinEventControl;
extern void simon_led_on(uint8_t led);
extern void simon_led_off(uint8_t led);
static uint32_t cntLongPress_btn0 = 0;
static bool handledLongPress_btn0 = false;

static uint32_t cntLongPress_btn1 = 0;
static bool handledLongPress_btn1 = false;

static uint32_t recallDelay = 0;

extern void emberAfPluginConnectionManagerStartSearchWithoutLeaveNetwork(void);

#ifdef BUTTON0
void emberAfPluginButtonInterfaceButton0HighCallback(void){
UTILS_PRINT(UPM_KEY, "key4 release\n");
}
void emberAfPluginButtonInterfaceButton0LowCallback(void){
	UTILS_PRINT(UPM_KEY, "key4 pressed\n");
    cntLongPress_btn0 = 0;

    handledLongPress_btn0 = false;
#if 0
    sceneConf_Stop();

    // debug
    printTablesDebug();
#endif
    //singleBlinkCtl_SysInitDone();

}
void emberAfPluginButtonInterfaceButton0PressedLongCallback(uint16_t timePressedMs, bool pressedAtReset){

UTILS_PRINT(UPM_KEY, "key4 pressedlong\n");
}
void emberAfPluginButtonInterfaceButton0PressedShortCallback(uint16_t timePressedMs){


	UTILS_PRINT(UPM_KEY, "key4 current attr=%d\n",_readAttr_OnOff(SAMPLE_SWITCH_KEY2_ENDPOINT));
	EmberAfStatus status=0;
	if(_readAttr_OnOff(SAMPLE_SWITCH_KEY2_ENDPOINT)==1){
		//simon_led_off(1);
		status=_writeAttr_OnOff(SAMPLE_SWITCH_KEY2_ENDPOINT,0);
		UTILS_PRINT(UPM_KEY, "key4 attr save res =%d\n",status);
		//_DrvWr_MLR(4 , 0);
	}else{
		//simon_led_on(1);
		status=_writeAttr_OnOff(SAMPLE_SWITCH_KEY2_ENDPOINT,1);
		UTILS_PRINT(UPM_KEY, "key4 attr save res =%d\n",status);
		//_DrvWr_MLR( 4, 1 );
	}
}
void emberAfPluginButtonInterfaceButton0PressingCallback(void){
UTILS_PRINT(UPM_KEY, "key4 pressing\n");
    if(cntLongPress_btn0 > 0){
        cntLongPress_btn0 += EMBER_AF_PLUGIN_BUTTON_INTERFACE_BUTTON_TIMEOUT_MS >> 2;
    }else{
        cntLongPress_btn0 = EMBER_AF_PLUGIN_BUTTON_INTERFACE_BUTTON_TIMEOUT_MS;
    }
    
    if(cntLongPress_btn0 >= MUC_DURATION_LEAVE_NWK){
        cntLongPress_btn0 = MUC_DURATION_LEAVE_NWK;
        if(!handledLongPress_btn0){
            switch(emberAfNetworkState()){
                case EMBER_JOINED_NETWORK:
                case EMBER_JOINED_NETWORK_NO_PARENT:
                    emberLeaveNetwork();
#if 0// lixp tmp
                    emberAfPluginConnectionManagerResetJoinAttempts();
                    // emberAfPluginConnectionManagerLeaveNetworkCallback();
                    emberEventControlSetDelayQS(emberAfPluginConnectionManagerRejoinEventControl,
                                                EMBER_AF_PLUGIN_CONNECTION_MANAGER_RETRY_TIME_S << 2);
#endif
                    UTILS_PRINT(UPM_KEY, "Nwk left, then will start rejoining in a minute.\n");
                    break;
                default:
                    break;
            }

            handledLongPress_btn0 = true;
        }
    }
}
#endif

#ifdef BUTTON1
void emberAfPluginButtonInterfaceButton1HighCallback(void){
UTILS_PRINT(UPM_KEY, "key1 release\n");
}
void emberAfPluginButtonInterfaceButton1LowCallback(void){
UTILS_PRINT(UPM_KEY, "key1 pressed\n");
#if 0
	if(MU_SYSMODE_NORMAL != mu_GetSysMode())
		return;

	sceneConf_Stop();

	uint32_t _tmCur = halCommonGetInt32uMillisecondTick();
	uint32_t _diff = _tmCur>=recallDelay?
			(_tmCur-recallDelay):
			(0xFFFFFFFF-recallDelay+_tmCur);

	if(_diff < MUC_DURATION_LIMIT_SCENE_RECALL)
		return ;

	switch(emberAfNetworkState()){
		case EMBER_JOINED_NETWORK:
			recallDelay = halCommonGetInt32uMillisecondTick();
			z3Ext_SceneRecall(1);
			singleBlinkCtl_Ptn_sceneRecall();
			z3Ext_ReportStdServerAttrToBind(1, 0x0001, 0x0021);
			break;
		case EMBER_JOINED_NETWORK_NO_PARENT:{
			extern void emberAfPluginConnectionManagerRejoinEventHandler(void);
			emberAfPluginConnectionManagerRejoinEventHandler();
			singleBlinkCtl_Ptn_NoNwk();
		} break;
		default:
			singleBlinkCtl_Ptn_NoNwk();
	}
#endif
    cntLongPress_btn1 = 0;
    handledLongPress_btn1 = false;

}
void emberAfPluginButtonInterfaceButton1PressedLongCallback(uint16_t timePressedMs, bool pressedAtReset){
UTILS_PRINT(UPM_KEY, "key1 pressed long\n");
}
void emberAfPluginButtonInterfaceButton1PressedShortCallback(uint16_t timePressedMs){
	UTILS_PRINT(UPM_KEY, "key1 pressed short=%d\n",_readAttr_OnOff(SAMPLE_SWITCH_ENDPOINT));
	//UTILS_PRINT(UPM_KEY, "key1 pressedlong=%d\n",emberAfContainsServer(SAMPLE_SWITCH_ENDPOINT,0x0006));
	if(_readAttr_OnOff(SAMPLE_SWITCH_ENDPOINT)==1){
		//simon_led_off(0);
		_writeAttr_OnOff(SAMPLE_SWITCH_ENDPOINT,0);
		//_DrvWr_MLR(3 , 0);
	}else{
		//simon_led_on(0);
		_writeAttr_OnOff(SAMPLE_SWITCH_ENDPOINT,1);
		//_DrvWr_MLR( 3, 1 );
	}
}
void emberAfPluginButtonInterfaceButton1PressingCallback(void){
UTILS_PRINT(UPM_KEY, "key1 pressed\n");
    if(cntLongPress_btn1 > 0){
        cntLongPress_btn1 += EMBER_AF_PLUGIN_BUTTON_INTERFACE_BUTTON_TIMEOUT_MS >> 2;
    }else{
        cntLongPress_btn1 = EMBER_AF_PLUGIN_BUTTON_INTERFACE_BUTTON_TIMEOUT_MS;
    }
    
    if(cntLongPress_btn1 >= MUC_DURATION_JOIN_NWK){
        cntLongPress_btn1 = MUC_DURATION_JOIN_NWK;
        if(!handledLongPress_btn1){
            emberAfPluginConnectionManagerResetJoinAttempts();
            if (!emberStackIsPerformingRejoin()) {
            	if(emberAfNetworkState() == EMBER_JOINED_NETWORK){
            		// Enter configuration mode.
            		UTILS_PRINT(UPM_KEY, "net statusis %d\n",emberAfNetworkState());
            		if(0 != sceneConf_Start()){
            			UTILS_PRINT(UPM_KEY, "An error occurred during invoking sceneConf_Start().\n");
            		}
            	}else
            		emberAfPluginConnectionManagerStartSearchWithoutLeaveNetwork();
            }else{
            	UTILS_PRINT(UPM_KEY, "Is performing rejoin...\n");
            }
            handledLongPress_btn1 = true;
        }
    }
}
#endif

#ifdef BUTTON2
void emberAfPluginButtonInterfaceButton2HighCallback(void){

}
void emberAfPluginButtonInterfaceButton2LowCallback(void){

	if(MU_SYSMODE_NORMAL != mu_GetSysMode())
		return;

	sceneConf_Stop();

	uint32_t _tmCur = halCommonGetInt32uMillisecondTick();
	uint32_t _diff = _tmCur>=recallDelay?
			(_tmCur-recallDelay):
			(0xFFFFFFFF-recallDelay+_tmCur);

	if(_diff < MUC_DURATION_LIMIT_SCENE_RECALL)
		return ;

	switch(emberAfNetworkState()){
		case EMBER_JOINED_NETWORK:
			recallDelay = halCommonGetInt32uMillisecondTick();
			z3Ext_SceneRecall(2);
			singleBlinkCtl_Ptn_sceneRecall();
			z3Ext_ReportStdServerAttrToBind(1, 0x0001, 0x0021);
			break;
		case EMBER_JOINED_NETWORK_NO_PARENT:{
			extern void emberAfPluginConnectionManagerRejoinEventHandler(void);
			emberAfPluginConnectionManagerRejoinEventHandler();
			singleBlinkCtl_Ptn_NoNwk();
		} break;
		default:
			singleBlinkCtl_Ptn_NoNwk();
	}
}
void emberAfPluginButtonInterfaceButton2PressedLongCallback(uint16_t timePressedMs, bool pressedAtReset){

}
void emberAfPluginButtonInterfaceButton2PressedShortCallback(uint16_t timePressedMs){

}
void emberAfPluginButtonInterfaceButton2PressingCallback(void){

}
#endif

#ifdef BUTTON3
void emberAfPluginButtonInterfaceButton3HighCallback(void){

}
void emberAfPluginButtonInterfaceButton3LowCallback(void){

	if(MU_SYSMODE_NORMAL != mu_GetSysMode())
		return;

	sceneConf_Stop();

	uint32_t _tmCur = halCommonGetInt32uMillisecondTick();
	uint32_t _diff = _tmCur>=recallDelay?
			(_tmCur-recallDelay):
			(0xFFFFFFFF-recallDelay+_tmCur);

	if(_diff < MUC_DURATION_LIMIT_SCENE_RECALL)
		return ;

	switch(emberAfNetworkState()){
		case EMBER_JOINED_NETWORK:
			recallDelay = halCommonGetInt32uMillisecondTick();
			z3Ext_SceneRecall(3);
			singleBlinkCtl_Ptn_sceneRecall();
			z3Ext_ReportStdServerAttrToBind(1, 0x0001, 0x0021);
			break;
		case EMBER_JOINED_NETWORK_NO_PARENT:{
			extern void emberAfPluginConnectionManagerRejoinEventHandler(void);
			emberAfPluginConnectionManagerRejoinEventHandler();
			singleBlinkCtl_Ptn_NoNwk();
		} break;
		default:
			singleBlinkCtl_Ptn_NoNwk();
	}
}
void emberAfPluginButtonInterfaceButton3PressedLongCallback(uint16_t timePressedMs, bool pressedAtReset){

}
void emberAfPluginButtonInterfaceButton3PressedShortCallback(uint16_t timePressedMs){

}
void emberAfPluginButtonInterfaceButton3PressingCallback(void){

}
#endif

#ifdef BUTTON4
void emberAfPluginButtonInterfaceButton4HighCallback(void){

}
void emberAfPluginButtonInterfaceButton4LowCallback(void){

	if(MU_SYSMODE_NORMAL != mu_GetSysMode())
		return;

	sceneConf_Stop();

	uint32_t _tmCur = halCommonGetInt32uMillisecondTick();
	uint32_t _diff = _tmCur>=recallDelay?
			(_tmCur-recallDelay):
			(0xFFFFFFFF-recallDelay+_tmCur);

	if(_diff < MUC_DURATION_LIMIT_SCENE_RECALL)
		return ;

	switch(emberAfNetworkState()){
		case EMBER_JOINED_NETWORK:
			recallDelay = halCommonGetInt32uMillisecondTick();
			z3Ext_SceneRecall(4);
			singleBlinkCtl_Ptn_sceneRecall();
			z3Ext_ReportStdServerAttrToBind(1, 0x0001, 0x0021);
			break;
		case EMBER_JOINED_NETWORK_NO_PARENT:{
			extern void emberAfPluginConnectionManagerRejoinEventHandler(void);
			emberAfPluginConnectionManagerRejoinEventHandler();
			singleBlinkCtl_Ptn_NoNwk();
		} break;
		default:
			singleBlinkCtl_Ptn_NoNwk();
	}
}
void emberAfPluginButtonInterfaceButton4PressedLongCallback(uint16_t timePressedMs, bool pressedAtReset){

}
void emberAfPluginButtonInterfaceButton4PressedShortCallback(uint16_t timePressedMs){

}
void emberAfPluginButtonInterfaceButton4PressingCallback(void){

}
#endif
